package com.copm.demonstrationsystem.service.modulemanagefeatures.impl;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.dao.modulemanagefeatures.ModuleBarDao;
import com.copm.demonstrationsystem.pojo.modulemanagefeatures.ModuleBar;
import com.copm.demonstrationsystem.service.modulemanagefeatures.ModuleManageFeaturesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chuyunfei
 * @date: 2018/4/17
 * @description: 功能模块管理的服务
 * 1、实现对指定模块的启用和禁用
 * 2、获取指定的模块列表
 */
@Service
public class ModuleManageFeaturesServiceImpl implements ModuleManageFeaturesService {

    //DAO层实现对象
    private ModuleBarDao moduleBarDao;

    @Override
    public ResponseStatus getMain(Boolean select){

        ResponseStatus response = new ResponseStatus();

        List<ModuleBar> bars = null;
        //是否进行筛选
        if(select){
            bars = moduleBarDao.getEnableMainBars();
        }else {
            bars = moduleBarDao.getMainBars();
        }
        //判断数据是否为空
        boolean empty = bars==null || bars.size() == 0;

        //设置返回状态
        response.setStatue(empty?0:1);

        if(empty){
            //设置错误信息
            response.setMessage("还没有模块");
        }else {
            //设置返回数据
            response.setData(bars);
        }
        return response;
    }

    @Override
    public ResponseStatus getSecond(Long id,Boolean select){

        ResponseStatus response = new ResponseStatus();

        List<ModuleBar> bars = null;
        //是否进行筛选
        if(select){
            bars = moduleBarDao.getEnableSecondBars(id);
        }else {
            bars = moduleBarDao.getSecondBars(id);
        }
        //判断数据是否为空
        boolean empty = bars==null || bars.size() == 0;

        response.setStatue(empty?0:1);

        if(empty){
            response.setMessage("该模块没有子模块");
        }else {
            response.setData(bars);
        }
        return response;
    }

    @Override
    @Transactional
    public ResponseStatus setModuleEnable(Long id,Boolean enable){

        ResponseStatus response = new ResponseStatus();

        //更新模块的启用状态
        boolean result = moduleBarDao.updateEnableOfBar(id,enable) == 1;

        //设置返回状态
        response.setStatue(result?1:0);

        if(!result){
            response.setMessage("设置模块的启用状态失败");
        }

        return response;
    }

    @Override
    @Transactional
    public ResponseStatus updateModuleName(Long id,String name){

        ResponseStatus response = new ResponseStatus();

        //更新模块的名称
        boolean result = moduleBarDao.updateNameOfBar(id,name) == 1;

        //设置返回状态
        response.setStatue(result?1:0);

        if(!result){
            response.setMessage("设置模块的名称失败");
        }

        return response;
    }

    @Autowired
    public void setModuleBarDao(ModuleBarDao moduleBarDao){
        this.moduleBarDao = moduleBarDao;
    }
}
